﻿namespace Yom.WebService.Admin
{
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using Yom.Common;

    public class AdminLink : AdminBase
    {
        public void OutWrite()
        {
            string Action = Factory.Request("Action");
            string ID = Factory.Request("ID");
            string LinkName = Factory.Request("LinkName");
            string LinkUrl = Factory.Request("LinkUrl");
            StringBuilder builder;

            switch (Action)
            {
                case "Add":
                    if (!string.IsNullOrEmpty(LinkName) && !string.IsNullOrEmpty(LinkUrl))
                    {
                        Yom.Model.Link model = new Yom.Model.Link();
                        model.IsCheck = true;
                        model.LinkName = LinkName;
                        model.LinkUrl = LinkUrl;
                        base.Response.Write((new Yom.Data.Link().Add(model) ? "1" : "0"));
                    }
                    else
                    {
                        builder = new StringBuilder();
                        builder.Append("<fieldset class=\"note\"><dl><dd>网站名称</dd>");
                        builder.Append("<dt><input id=\"linkname\" type=\"text\" class=\"txt\" /></dt></dl>");
                        builder.Append("<dl><dd>网站地址</dd>");
                        builder.Append("<dt><input id=\"linkurl\" type=\"text\" class=\"txt\" /></dt></dl>");
                        builder.Append("</fieldset><div id=\"ErrShow\" style=\"color:red\"></div><div class=\"SubButton\"><input type=\"button\" id=\"Submit\" value=\"添加链接\" class=\"button\"/></div>");
                        base.Response.Write(builder.ToString());
                    }
                    break;
                case "Edit":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        Yom.Model.Link model = new Yom.Data.Link().GetModel(ID.ToInt());
                        if (!string.IsNullOrEmpty(LinkName) && !string.IsNullOrEmpty(LinkUrl))
                        {
                            model.LinkName = LinkName;
                            model.LinkUrl = LinkUrl;
                            base.Response.Write((new Yom.Data.Link().Update(model) ? "1" : "0"));
                        }
                        else
                        {
                            builder = new StringBuilder();
                            builder.Append("<fieldset class=\"note\"><dl><dd>网站名称</dd>");
                            builder.Append("<dt><input id=\"linkname\" type=\"text\" class=\"txt\" value=\"" + model.LinkName + "\" /></dt></dl>");
                            builder.Append("<dl><dd>网站地址</dd>");
                            builder.Append("<dt><input id=\"linkurl\" type=\"text\" class=\"txt\" value=\"" + model.LinkUrl + "\" /></dt></dl>");
                            builder.Append("</fieldset><div id=\"ErrShow\" style=\"color:red\"></div><div class=\"SubButton\"><input type=\"button\" id=\"Submit\" value=\"修改链接\" class=\"button\"/></div>");
                            base.Response.Write(builder.ToString());
                        }
                    }
                    break;
                case "Accept":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        base.Response.Write((new Yom.Data.Link().SetCheck(ID.ToInt(), true)));
                    }
                    break;
                case "Canel":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        base.Response.Write((new Yom.Data.Link().SetCheck(ID.ToInt(), false)));
                    }
                    break;
                case "Del":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        base.Response.Write((new Yom.Data.Link().Delete(ID.ToInt()) ? "1" : "0"));
                    }
                    break;
                default:
                    builder = new StringBuilder();
                    builder.Append("<div class=\"SubMenu\"><a href=\"javascript:$.Run(10)\">链接管理</a> | <a href=\"javascript:$.Run(10,1)\">添加链接<a/></div>");
                    builder.Append("<div align=\"left\" style=\"padding:5px;\">");
                    builder.Append("<table border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"TablePanel\">");
                    builder.Append("<tr align=\"center\" class=\"TDHead\">");
                    builder.Append("<td nowrap>网站名称</td><td nowrap>链接地址</td><td nowrap>&nbsp;</td></tr>");
                    List<Yom.Model.Link> list = new Yom.Data.Link().GetList();
                    foreach (Yom.Model.Link model in list)
                    {
                        builder.Append("<tr>");
                        builder.AppendFormat("<td align=\"center\"><a href=\"javascript:$.Run(9,2,{0})\">{1}</a></td>", model.LinkID, model.LinkName);
                        builder.AppendFormat("<td align=\"center\"><a href=\"{0}\" target=\"_blank\">{0}</a></td>", model.LinkUrl);
                        builder.AppendFormat("<td align=\"center\"><img border=\"0\" height=\"16\" width=\"16\" src=\"Images/Admin/{0}.png\" alt=\"{1}\" style=\"cursor:pointer\" onclick=\"$.Run(9,{3},{2})\" />&nbsp;<img src=\"Images/Admin/del.gif\" border=\"0\" width=\"16\" height=\"16\" alt=\"删除链接\" onclick=\"$.Run(9,3,{2})\" style=\"cursor:pointer\" /></td>", (model.IsCheck ? "canel" : "accept"), (model.IsCheck ? "隐藏" : "显示"), model.LinkID, (model.IsCheck ? "5" : "4"));
                        builder.Append("</tr>");
                    }
                    builder.Append("</table><br /><div id=\"ShowMsg\" style=\"color:red\"></div>");
                    base.Response.Write(builder.ToString());
                    break;
            }
        }
    }
}